package com.tiankong.product.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.tiankong.product.entity.Brand;
import com.tiankong.product.mapper.BrandMapper;
import com.tiankong.product.service.BrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 品牌 服务实现类
 * </p>
 *
 * @author scl
 * @since 2021-03-22
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    @Override
    public void saveBrand(Brand brand) {
        QueryChainWrapper<Brand> wrapper = query().eq("name", brand.getName());
        if (brand.getBrandId() != null){
            wrapper.ne("brand_id",brand.getBrandId());
        }
        Integer count = wrapper.count();
        if (count >0){
            throw new DuplicateKeyException(String.format("【%s】已存在",brand.getName()));
        }
        super.saveOrUpdate(brand);
    }
}
